Function

  • Function

    
                           <?php 
                                    //variable for student 
                                    $student1 =["name" => "manoj", "mark" => 90, 'total_fee' => 5000, 'paid'=>1000, 'income'=>10000];                                
                                    $marks = $student1['marks'];
                                    if ($marks>=60)
                                    {
                                          $grade = "First Division";
                                    }
                                    else if($marks>=45)
                                    {
                                          $grade = "Second Division";
                                    }
                                    else if($marks>=33)
                                    {
                                          $grade = "Third Division";
                                    }
                                    else
                                    {
                                          $grade = "Fail";
                                    }                                
                                    $pending_fee=$student1['total_fee']-$student1['paid'];
                                    if($student1['income']>2000){
                                        echo 'Not eligible for scholarship';
                                    }
                                    else{
                                        echo 'Eligible for scholarship';
                                    }                             
                                    $teacher1 =["name" => "manoj", "age" => 45, 'basic' => 5000, 'ta'=>1000, 'hra'=>10000];
                                    $total_salary= $teacher1['basic']+$teacher1['ta']+$teacher1['hra'];
                                    if( $teacher1['age']>55){
                                       echo 'Retirement Age';
                                    }
                                    $bus1 =["no" => "kL0723", "model" => 2000, 'insurance' => '2023-08-10']; 
                                    $next_test=2023-$bus1['model'];
                                    if($next_test>15){
                                        echo 'Need re-test';
                                    }
                                    
    
    
                                    ?>
    
                       
    Note that the above code, we have implemented logic of three tasks, 1) find grade, 2) find pending fee , 3) find scholoarship eligibility
    In this approach , the code maintance , modification and dubugging are not easy.
    Define separate functions based on each logic for making the code easier to maintain, modify and debug
    
                           <?php 
                            function findGrade($marks){
    
                                    if ($marks>=60)
                                    {
                                          $grade = "First Division";
                                    }
                                    else if($marks>=45)
                                    {
                                          $grade = "Second Division";
                                    }
                                    else if($marks>=33)
                                    {
                                          $grade = "Third Division";
                                    }
                                    else
                                    {
                                          $grade = "Fail";
                                    }
    
                                    return $grade;
    
                            } 
                            function findFee($total_fee, $paid ){
                                $pending_fee=$total_fee-$paid;
                            }
                            function findScholarship($income ){
                                   if($income>2000){
                                        echo 'Not eligible for scholarship';
                                    }
                                    else{
                                        echo 'Eligible for scholarship';
                                    }
                            }
    
                            function findSalary(){
    
                            }
    
                            function findRetirement(){
    
                            }
    
                            function findRenewal(){
                                  
                            }
                            
    
    
    
                          
                            ?>